package algorithm.easy;

import pojo.ListNode;
import pojo.TreeNode;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 江岸
 * @version V1.0
 * @ClassName: MergeTwoLists21
 * @description: 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 * @date 2021/1/189:52
 * @since V1.0
 */
public class MergeTwoLists21 {

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode();
        ListNode listNode2 = new ListNode();
        mergeTwoLists(null, null);
    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        List<ListNode> arr1 = new ArrayList<>();
        while (true) {
            arr1.add(l1);
            if (l1.next != null) {
                l1 = l1.next;
            } else {
                break;
            }
        }
        List<ListNode> arr2 = new ArrayList<>();
        while (true) {
            arr2.add(l2);
            if (l2.next != null) {
                l2 = l2.next;
            } else {
                break;
            }
        }
        ArrayList<ListNode> list = new ArrayList();
        int i = 0;
        int j = 0;
        while (true) {
            if (arr1.get(i).val <= arr2.get(j).val) {
                list.add(arr1.get(i));
                if (i < arr1.size() - 1) {
                    i++;
                } else if (i == arr1.size() - 1) {
                    list.add(arr2.get(j));
                    break;
                }
            } else {
                list.add(arr2.get(j));
                if (j < arr2.size() - 1) {
                    j++;
                } else if (j == arr2.size() - 1) {
                    list.add(arr1.get(i));
                    break;
                }
            }
        }
        for (int n = 0; n < list.size() - 1; n++) {
            list.get(n).next = list.get(n + 1);
        }
        return list.get(0);
    }

    //答案 递归
    public ListNode answer(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }

    }


    public ListNode mergeTwoLists2(ListNode list1, ListNode list2) {
        ListNode res = new ListNode();
        ListNode temp = res;
        while (true) {
            if (list1 == null) {
                temp.next = list2;
                break;
            }
            if (list2 == null) {
                temp.next = list1;
                break;
            }
            if (list1.val < list2.val) {
                temp.next = list1;
                list1 = list1.next;
            }else {
                temp.next = list2;
                list2 = list2.next;
            }
            temp = temp.next;
        }
        return res.next;

    }
}

